#include "component_right_top_button_group.h"
#include "../common/utils.h"

struct _ComponentRightTopButtonGroup
{
    GtkBox parent_instance;
    GtkWidget *button_minimize;
    GtkWidget *button_toggle_maximize;
    GtkWidget *button_close;
};

static void button_minimize_clicked_callback(gpointer data)
{
    ComponentRightTopButtonGroup *self = COMPONENT_RIGHT_TOP_BUTTON_GROUP(data);
    GtkWidget *window = gtk_widget_get_ancestor(GTK_WIDGET(self), GTK_TYPE_WINDOW);
    gtk_window_minimize(GTK_WINDOW(window));
}

static void component_right_top_button_group_set_is_maximized(ComponentRightTopButtonGroup *self, gboolean is_maximized)
{
    if (is_maximized)
    {
        gtk_button_set_icon_name(GTK_BUTTON(self->button_toggle_maximize), "view-paged");
    }
    else
    {
        gtk_button_set_icon_name(GTK_BUTTON(self->button_toggle_maximize), "media-playback-stop");
    }
}

static void button_toggle_maximize_clicked_callback(gpointer data)
{
    ComponentRightTopButtonGroup *self = COMPONENT_RIGHT_TOP_BUTTON_GROUP(data);
    GtkWidget *window = gtk_widget_get_ancestor(GTK_WIDGET(self), GTK_TYPE_WINDOW);
    if (gtk_window_is_maximized(GTK_WINDOW(window)))
    {
        gtk_window_unmaximize(GTK_WINDOW(window));
        component_right_top_button_group_set_is_maximized(self, FALSE);
    }
    else
    {
        gtk_window_maximize(GTK_WINDOW(window));
        component_right_top_button_group_set_is_maximized(self, TRUE);
    }
}

static void button_close_clicked_callback(gpointer data)
{
    ComponentRightTopButtonGroup *self = COMPONENT_RIGHT_TOP_BUTTON_GROUP(data);
    GtkWidget *window = gtk_widget_get_ancestor(GTK_WIDGET(self), GTK_TYPE_WINDOW);
    gtk_window_destroy(GTK_WINDOW(window));
}

G_DEFINE_TYPE(ComponentRightTopButtonGroup, component_right_top_button_group, GTK_TYPE_BOX)

static void component_right_top_button_group_class_init(ComponentRightTopButtonGroupClass *klass)
{
    gtk_widget_class_set_template_from_resource(GTK_WIDGET_CLASS(klass), "/src/components/component_right_top_button_group.ui");
    utils_load_css_from_resource("/src/components/component_right_top_button_group.css");
    gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), ComponentRightTopButtonGroup, button_minimize);
    gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), ComponentRightTopButtonGroup, button_toggle_maximize);
    gtk_widget_class_bind_template_child(GTK_WIDGET_CLASS(klass), ComponentRightTopButtonGroup, button_close);
    gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(klass), button_minimize_clicked_callback);
    gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(klass), button_toggle_maximize_clicked_callback);
    gtk_widget_class_bind_template_callback(GTK_WIDGET_CLASS(klass), button_close_clicked_callback);
}

static void component_right_top_button_group_init(ComponentRightTopButtonGroup *self)
{
    gtk_widget_init_template(GTK_WIDGET(self));
}
